package sui.utils.encrypt;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class AESUtils {
        /**
         * 算法名称 
         */
	 public static final String KEY_ALGORITHM = "AES";
	 // public static final String KEY_ALGORITHM = "DES";
	  
	  /**
	  * 加密、解密/ 工作模式/ 填充方式
	  */
	  public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
	  //public static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";
	  
	  /**
	     *   
	     * 生成密钥key对象
	     * @param KeyStr 密钥字符串 
	     * @return 密钥对象 
	     * @throws InvalidKeyException   
	     * @throws NoSuchAlgorithmException   
	     * @throws InvalidKeySpecException   
	     * @throws Exception 
	     */
	    private static SecretKey keyGenerator(String keyStr) throws Exception {
	        byte input[] =Base64.decodeBase64(keyStr);
	      //实例化AES秘钥材料
	        SecretKey secretKey = new SecretKeySpec(input, KEY_ALGORITHM);
	        return secretKey;
	    }

	    /** 
	     * 加密数据
	     * @param data 待加密数据
	     * @param key 密钥
	     * @return 加密后的数据 
	     */
	    public static String encrypt(String data, String key) throws Exception {
	        Key deskey = keyGenerator(key);
	      //实例化
	        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
	        //初始化，设置解密模式
	        cipher.init(Cipher.ENCRYPT_MODE,deskey);
	        //执行操作 
	        byte[] results = cipher.doFinal(data.getBytes());
	        // 执行加密操作。加密后的结果通常都会用Base64编码进行传输 
	        return Base64.encodeBase64String(results);
	    }
	  
	    /** 
	     * 解密数据 
	     * @param data 待解密数据 
	     * @param key 密钥 
	     * @return 解密后的数据 
	     */
	    public static String decrypt(String data, String key) throws Exception {
	        Key deskey = keyGenerator(key);
	        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
	        //初始化Cipher对象，设置为解密模式
	        cipher.init(Cipher.DECRYPT_MODE, deskey);
	        // 执行解密操作 解密后的结果通常都会用Base64编码进行传输 
	        return new String(cipher.doFinal(Base64.decodeBase64(data)));
	    }
	    
	    /**
	    * 生成秘密秘钥
	    * java7 只支持56位密钥
	    * Bouncy Castle 支持64位秘密
	    * @return 二进制秘钥
	    * @throws Exception
	    */
	    public static String initKey() throws Exception {
	    	/**
	    	* 实例化秘钥生成器
	    	*如要使用64位秘钥需要替换为 KeyGenerator.getInstance(CIPHER__ALGORITHM, "BC")；
	    	*"BC"是Bouncy Castle安全提供者的缩写。
	    	*/
	    	//初始化秘钥生成器
	    	KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
	    	//KeyGenerator kg= KeyGenerator.getInstance(CIPHER_ALGORITHM, "BC");
	    	//生成秘密秘钥
	    	kg.init(128); //must be equal to 128, 192 or 256
	    	//获得秘钥的二进制编码形式
	    	SecretKey secretKey = kg.generateKey();
	    	//结果通常都会用Base64编码进行传输 
	    	return Base64.encodeBase64String(secretKey.getEncoded());
	    }
	    
	    public static void main(String[] args) throws Exception{
	    	String source="RhLNGDVrJctqTbkAUY7/Ig==";
	    	System.out.println("原文：\t"+ source);
	    	String key = AESUtils.initKey(); 
	    	System.out.println("秘钥：\t" + key);
	    	//加密
	    	Long encryptStartTime=System.currentTimeMillis();
	    	String inputStr = AESUtils.encrypt(source, key);
	    	System.out.println("加密后：\t" + inputStr);
	    	 long encryptEndTime = System.currentTimeMillis(); 
	    	 System.out.println("加密耗时"+(encryptEndTime - encryptStartTime) +"SSS");
	    	//解密
	    	 long decryptStartTime = System.currentTimeMillis(); 
	    	String outputStr = AESUtils.decrypt(inputStr, key);
	    	System.out.println("解密后：\t" + outputStr);
	    	long decryptEndTime = System.currentTimeMillis();
	    	 System.out.println("解密密耗时"+(decryptEndTime - decryptStartTime) +"SSS");
	    	Boolean bool = source.equals(outputStr);
	    	System.out.println(bool);
		}
}
